package com.lanhe.apply.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.apply.domain.LhQingchuangApplyPool;
import com.lanhe.apply.mapper.LhQingchuangApplyPoolMapper;
import com.lanhe.apply.service.ILhQingchuangApplyPoolService;
import com.lanhe.common.constant.HttpStatus;
import com.lanhe.common.exception.ServiceException;
import com.lanhe.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * (LhQingchuangApplyPool)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-19 21:57:09
 */
@Service
public class LhQingchuangApplyPoolServiceImpl extends ServiceImpl<LhQingchuangApplyPoolMapper, LhQingchuangApplyPool> implements ILhQingchuangApplyPoolService {

	@Autowired
	private LhQingchuangApplyPoolMapper applyPoolMapper;

	/**
	 * 更新汇总数据
	 *
	 * @param applyPool 实体
	 * @return 结果
	 * @author zhanggl
	 * @date 2023年12月19日22:05:31
	 */
	@Override
	public LhQingchuangApplyPool saveApply(LhQingchuangApplyPool applyPool) {
		if (StringUtils.isBlank(applyPool.getNumber())) {
			throw new ServiceException("产品编号不能为空", HttpStatus.ERROR);
		}
		if (ObjectUtil.isNull(applyPool.getMemberId())) {
			throw new ServiceException("用户ID不能为空", HttpStatus.ERROR);
		}
		LambdaQueryWrapper<LhQingchuangApplyPool> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhQingchuangApplyPool::getNumber, applyPool.getNumber())
				.eq(LhQingchuangApplyPool::getMemberId, applyPool.getMemberId());
		LhQingchuangApplyPool lhQingchuangApplyPool = applyPoolMapper.selectOne(queryWrapper);
		if (ObjectUtil.isNull(lhQingchuangApplyPool)){
			applyPool.setCreateTime(new Date());
			applyPool.setApplyNumber(1L);
			applyPoolMapper.insert(applyPool);
			return applyPool;
		} else {
			applyPool.setUpdateTime(new Date());
			applyPool.setApplyNumber(lhQingchuangApplyPool.getApplyNumber()+1);
			applyPoolMapper.update(applyPool,queryWrapper);
			return lhQingchuangApplyPool;
		}
	}
}

